package com.css.zfzx.sjcj.modules.yhdisaster.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.yhdisaster.repository.entity.YhDisasterEntity;
import com.css.zfzx.sjcj.modules.yhdisaster.service.YhDisasterService;
import com.css.zfzx.sjcj.modules.yhdisaster.viewobjects.CheckVO;
import com.css.zfzx.sjcj.modules.yhdisaster.viewobjects.YhDisasterQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author lxg
 * @date 2020-11-10
 */
@Slf4j
@RestController
@RequestMapping("/yh/yhDisasters")
public class YhDisasterController {
    @Autowired
    private YhDisasterService yhDisasterService;

    @GetMapping("/queryYhDisasters/{type}")
    public RestResponse queryYhDisasters(@PathVariable("type")String type, HttpServletRequest request, YhDisasterQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort=request.getParameter("sort");
            String order=request.getParameter("order");
            JSONObject jsonObject = yhDisasterService.queryYhDisasters(queryParams,curPage,pageSize,type,sort,order);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getYhDisaster(@PathVariable String id) {
        RestResponse response = null;
        try{
            YhDisasterEntity yhDisaster = yhDisasterService.getYhDisaster(id);
            response = RestResponse.succeed(yhDisaster);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("{type}")
    public RestResponse saveYhDisaster(@PathVariable String type,@RequestBody YhDisasterEntity yhDisaster) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            yhDisasterService.saveYhDisaster(yhDisaster,type);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    /**
     * 修改数据
     * @param yhDisaster
     * @return
     */
    @PostMapping("/update")
    public RestResponse updateYhDisaster(@RequestBody YhDisasterEntity yhDisaster)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            yhDisasterService.updateYhDisaster(yhDisaster);
            json.put("message", "质检成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "质检失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     *批量删除数据
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public RestResponse deleteYhDisasters(@RequestBody String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            List<String> split = Arrays.asList(ids.split(","));
            yhDisasterService.deleteYhDisasters(split);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            e.printStackTrace();
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取有效的字典项
     * @param dictCode
     * @return
     */
    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(yhDisasterService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
    /**
     * 导入
     *
     * @param file
     * @param response
     * @return
     */
    @PostMapping("/importDisaster")
    public RestResponse importDisaster( @RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = yhDisasterService.importExcel( file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 提交数据
     * @param ids
     * @return
     */
    @PostMapping("/submit")
    public RestResponse submitYhDisaster(@RequestBody String ids)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            yhDisasterService.submitYhDisaster(ids);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 导出
     * @param type
     * @param response
     * @return
     */
    @GetMapping("/exportFile/{type}")
    public RestResponse exportFileYhDisasters(@PathVariable("type")String type, HttpServletResponse response,
                                              @RequestParam("yhCode") String yhCode,@RequestParam("yhUnit") String yhUnit,
                                              @RequestParam("yhProjectName")String yhProjectName,@RequestParam("yhProjectTask") String yhProjectTask,
                                              @RequestParam("province") String province,@RequestParam("city")String city,@RequestParam("area")String area,
                                              @RequestParam("yhConstructionScale") String yhConstructionScale,@RequestParam("yhEarthFortification")String yhEarthFortification,
                                              @RequestParam("yhFieldType")String yhFieldType,@RequestParam("yhLotType")String yhLotType,@RequestParam("qualityResult")String qualityResult,@RequestParam(required = false,value = "extends1")String extends1) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try{
            YhDisasterQueryParams queryParams=new YhDisasterQueryParams();
            if(!("null".equals(province) || StringUtils.isEmpty(province))){
                queryParams.setProvince(province);
            }
            if(!("null".equals(city) || StringUtils.isEmpty(city))){
                queryParams.setCity(city);
            }
            if(!("null".equals(area) || StringUtils.isEmpty(area))){
                queryParams.setArea(area);
            }if(!("null".equals(extends1) || StringUtils.isEmpty(extends1))){
                queryParams.setExtends1(extends1);
            }
            queryParams.setYhCode(yhCode);
            queryParams.setYhConstructionScale(yhConstructionScale);
            queryParams.setYhEarthFortification(yhEarthFortification);
            queryParams.setYhFieldType(yhFieldType);
            queryParams.setYhLotType(yhLotType);
            queryParams.setYhProjectName(yhProjectName);
            queryParams.setYhProjectTask(yhProjectTask);
            queryParams.setYhUnit(yhUnit);
            queryParams.setQualityResult(qualityResult);
            yhDisasterService.exportFile(queryParams,type,response);
            jsonObject.put("message", "导出成功");
            responseRest = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }

    @PostMapping("/spAllForm")
    public RestResponse listCheck(@RequestBody CheckVO checkVO)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            yhDisasterService.listCheck(checkVO);
            json.put("message", "质检成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "质检失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    /***
     * 系统自动质检
     * @param type
     * @return
     */
    @PostMapping("/dataQualityCheck/{type}")
    public RestResponse dataQualityCheck(@PathVariable("type")String type,
                                         @RequestBody YhDisasterQueryParams queryParams) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try{
            yhDisasterService.dataQualityCheck(queryParams,type);
            jsonObject.put("message", "质检成功！");
            responseRest = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "质检失败!";
            log.error(errorMessage,e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }
}